package com.ts.api.http.market.channel;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.encode.Base64Utils;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.module.api.convert.core.annotation.ApiChannelMarket;
import com.ts.api.module.api.convert.core.annotation.ApiChannelMarketConvertType;
import com.ts.api.module.api.convert.core.entity.ApiBizStatus;
import com.ts.api.module.api.convert.market.entity.ChannelMarketParameter;
import com.ts.api.module.api.convert.market.handler.ChannelMarketConvertHandler;
import com.ts.api.module.api.entity.ApiUserRequest;

import java.util.Map;

/**
 *  有钱来(白天)
 *  【有钱钱包对接文档】链接: https://apifox.com/apidoc/shared-64fcec58-85e4-409f-aafd-e2b8776dbe3f?pwd=youqian
 *  @author WJB
 */
@ApiChannelMarket(uniqueCode = "e04570c2419e406e94e8b695cad84479", type = ApiChannelMarketConvertType.FEDERATE_LOGIN)
public class YouQianLaiBaiFederate implements ChannelMarketConvertHandler {

    public static final String KEY = "oYvaWJmNYRk760W9gKQ9Lg==";

    @Override
    public ApiUserRequest parse(RequestVO requestVO) {
        String body = requestVO.getStr("params");
        AES cbc = AES.createCbc(Base64Utils.decode(KEY), Base64Utils.decode(KEY));
        String decrypt = cbc.decryptFromBase64Str(body);
        JSONObject object = JSONObject.parseObject(decrypt);
        String mobile = object.getString("mobile");

        ApiUserRequest apiUserRequest = new ApiUserRequest();
        apiUserRequest.setPhone(mobile);
        return apiUserRequest;
    }

    @Override
    public Map<String, Object> response(ChannelMarketParameter channelMarketParameter, ApiBizStatus apiBizStatus) {
        if (apiBizStatus.hasBizSuc()) {
            return MapUtils.newHashMap(newMap -> {
                newMap.put("code", 200);
                newMap.put("msg", apiBizStatus.getBizMsg());
                newMap.put("data", MapUtils.newLinkedHashMap(dataMap -> {
                    dataMap.put("url", channelMarketParameter.getUrl());
                }));
            });
        }

        return MapUtils.newHashMap(newMap -> {
            newMap.put("code", 500);
            newMap.put("msg", apiBizStatus.getBizMsg());
        });
    }
}
